package com.renderedideas.jailescape;

import com.renderedideas.gamemanager.GameManager;
import com.renderedideas.gamemanager.GameObject;
import com.renderedideas.gamemanager.GameObjectManager;
import com.renderedideas.gamemanager.GameView;
import com.renderedideas.gamemanager.MessageQueue;
import com.renderedideas.gamemanager.Point;
import com.renderedideas.gamemanager.TileMap;
import com.renderedideas.gamemanager.Utility;
import com.renderedideas.platform.Bitmap;
import com.renderedideas.platform.Debug;
import com.renderedideas.platform.Music;
import com.renderedideas.platform.PlatformService;
import javax.microedition.lcdui.Graphics;

/**
 *
 * @author Ramiz
 */
public class GamePlayView extends GameView
{

    private Point variablePosOfJoystick;

    String message = "";
    public TileMap map;
    public Player player;
    GameObjectManager gom;

    public static GamePlayView instance;
    public static boolean moveLeft;
    public static boolean moveRight;
    public static boolean moveUp;
    public static boolean moveDown;

    private Point mousePointerPosition;
    private Bitmap mousePointer;
    private final int MOUSE_SPEED = 6;
    //-----------------------for mouse update-------------------//
    private boolean leftMouseupdate;
    private boolean rightMouseUpdate;
    private boolean upMouseUpdate;
    private boolean downMouseUpdate;
    //-----------------------------------------------------------//
    //------------------for map viewing----------------------//
    private boolean leftMapUpdate;
    private boolean rightMapUpdate;
    private boolean upMapUpdate;
    private boolean downMapUpdate;
    //--------------------------------------------------------//

    private Bitmap loadingScreenBackground;
    public int currentLevel;
    public int currentScreen;

    private Point resumeButtonPositionPauseScreen, restartButtonPositionPauseScreen, quitButtonPositionPauseScreen, controlsButtonPositionPauseMenu;

    private Point restartPositionGameOverScreen, quitPositionGameOverScreen;

    private Music gameMusic, AlarmSiren;

    private Point controller_left, controller_right, controller_up, controller_down, controller_punch;
    private Point controller_touched = new Point(0, 0);

    private static Point controllerJoystickRing, controllerJoystickInnerPart, controllerJoystickInnerDefaultPos;
    private Point controllerWRTPlayer;
    private Point pauseButtonPosition;

    private int progressBarWidth = 0;
    private int progressBarSize = GameManager.screenWidth * 58 / 100;
    private boolean usingJoystick;

    private int joystickOuterRadius;
    private boolean blinking = true;

    //#if ScreenWidth == 640 && ScreenHeight == 360
    //# int tileWidth = 51;
    //# int tileHeight = 51;
    //#elif ScreenWidth == 128 && ScreenHeight == 160
    //# int tileWidth = 16;
    //# int tileHeight = 16;
    //#elif ScreenWidth == 320 || ScreenWidth == 240 || ScreenWidth == 400
    int tileWidth = 32;
    int tileHeight = 32;
        //#endif

    //------------------------used in freedom screen ------------------//
    private Point backButtonPosition = new Point(0, 0);
//        private Point aboutButtonPosition = new Point(0, 0);
    //-----------------------------------------------------------------//

    //---------------used in loading completed screen & for delay before gameover-----//
    private long startTime, currentTime;
    private final int BLINK_TIME = 700;
        //-------------------------------------------------------------------------//

    //-----------------for map viewing mode-storing positions of player and map --------------//
    int tempStartX = 0;
    int tempStartY = 0;
    Point playerPosition = new Point(0, 0);
    //-----------------------------------------------------------------------------//

    private Point mapViewModeIconPoint;

    private int MAP_SCROLL_SPEED = GameManager.screenWidth * 1 / 100;
    private boolean controller_pressed = false;
    private int FREEDOM_LEVEL = 49;

    private String[][] tipstring;
    private int tipIndex;
    public static long timeLimit;                  //----------alarm max time--------------//
    private long time;                                        //-----------time remaing to game over---------//

    MessageQueue tapControllerMessage;

    //-------------used for map update in mapview mode----------------------//
    private boolean moveButtonDown;
    private boolean moveButtonUp;
    private boolean moveButtonRight;
    private boolean moveButtonLeft;
    //-------------------------------------------------//
    private int navKeyTouchPointerID;                 //-----------------------------pointer id for navigation keys--------------//
    private int punchTouchPointerID;                //--------------punch button pointer id--------------------//

    private Bitmap freedomBackground;
    private Bitmap backButton;

    private boolean punchButtonDown;
    private boolean moveJoystickButton;
    private Music gameOverMusic;

    public boolean isCopInRange;

    public GamePlayView()
    {
        try
        {
            // currentLevel = 1;
            currentLevel = LevelSelectView.levelStartCount;
            loadingScreenBackground = new Bitmap("/images/menu/background.png");
            loadTips();
            currentScreen = Constants.SCREEN_LOADING_SCREEN;
            tapControllerMessage = new MessageQueue();
            instance = this;
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }

    }

    public void loadMusic()
    {
        try
        {
            gameMusic = new Music(100, "/music/gameplay", -1);
            if (Game.isSoundEnabled && gameMusic != null)
            {
                gameMusic.play();
            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }

    public void loadGame()
    {
        progressBarWidth = 25 * progressBarSize / 100;
        BitmapCacher.preloadAllBitmap();
        mousePointer = BitmapCacher.mousePointer;
        progressBarWidth = 45 * progressBarSize / 100;

        gom = new GameObjectManager();
        progressBarWidth = 65 * progressBarSize / 100;

        resumeButtonPositionPauseScreen = new Point(GameManager.screenWidth / 2 - BitmapCacher.resume.getWidth() / 2, GameManager.screenHeight * 20 / 100 - BitmapCacher.resume.getHeight() / 2);
        restartButtonPositionPauseScreen = new Point(GameManager.screenWidth / 2 - BitmapCacher.replay.getWidth() / 2, GameManager.screenHeight * 40 / 100 - BitmapCacher.replay.getHeight() / 2);
        quitButtonPositionPauseScreen = new Point(GameManager.screenWidth / 2 - BitmapCacher.quit.getWidth() / 2, GameManager.screenHeight * 60 / 100 - BitmapCacher.quit.getHeight() / 2);
        controlsButtonPositionPauseMenu = new Point(GameManager.screenWidth / 2 - BitmapCacher.pauseControlsSetting.getWidth() / 2, GameManager.screenHeight * 80 / 100 - BitmapCacher.pauseControlsSetting.getHeight() / 2);

        progressBarWidth = 75 * progressBarSize / 100;
        restartPositionGameOverScreen = new Point(GameManager.screenWidth / 2 - BitmapCacher.replay.getWidth() / 2, GameManager.screenHeight / 2);
        quitPositionGameOverScreen = new Point(GameManager.screenWidth / 2 - BitmapCacher.quit.getWidth() / 2, GameManager.screenHeight * 75 / 100);
        if (PlatformService.isTouchSupported())
        {
            initialiseControllerPoint();
        }

        progressBarWidth = 85 * progressBarSize / 100;

        mousePointerPosition = new Point(GameManager.screenWidth / 2, GameManager.screenHeight / 2);
        pauseButtonPosition = new Point(GameManager.screenWidth - BitmapCacher.pause_button.getWidth(), 0);

        mapViewModeIconPoint = new Point(0, 0);

        tileMapSetup();

        loadMusic();
        progressBarWidth = 100 * progressBarSize / 100;
        currentScreen = Constants.SCREEN_LOADING_COMPLETED_SCREEN;
        GameManager.isLoadingScreen = false;
    }

    private void loadTips()
    {

        tipstring = new String[][]
        {
            {
                "find doorkey in map", "to unlock the door"
            },
            {
                "Find camera lever to", "off the fixed cameras"
            },
            {
                "find laser off button ", "to turn off lasers"
            },
            {
                "Dont get caught by ", "petroling officers"
            },
            {
                "Some guards with GUN ", "can shoot you"
            },
            {
                "Use 'MapViewMode'", "if you're stuck"
            },
            {
                "Guards will be unconsicious", "if you punch them"
            },
            {
                "Like us on facebook", "for more upcoming games"
            },
            //#if ScreenWidth == 320 || ScreenWidth == 400 || ScreenWidth == 360
            {
                "You can choose different", " controllers anytime "
            },
            {
                "You can reposition joystick", "as per your convenience"
            }
        //#endif
        };

        tipIndex = PlatformService.nextInt(tipstring.length);
    }

    public void tileMapSetup()
    {
        map = new TileMap(MapInfo.getMapDim(currentLevel).x, MapInfo.getMapDim(currentLevel).y, tileWidth, tileHeight, "/maps/map" + currentLevel);
        map.CreateSprites(map.maxX, map.maxY);
        player = new Player(map.getRequestedTileFromWorld(Constants.PLAYER_SPAWN));

        int spawnTileX = map.getRequestedTileFromWorld(Constants.PLAYER_SPAWN).x;
        int spawnTileY = map.getRequestedTileFromWorld(Constants.PLAYER_SPAWN).y;

        map.MoveMapX(spawnTileX - GameManager.screenWidth / 2);
        map.MoveMapY(spawnTileY - GameManager.screenHeight / 2);

        player.position.x = spawnTileX - map.startX;
        player.position.y = spawnTileY - map.startY;
    }

    public void initialiseControllerPoint()
    {
        controller_left = new Point(0, GameManager.screenHeight - BitmapCacher.controller_left.getHeight() * 250 / 100);
        controller_right = new Point(BitmapCacher.controller_right.getWidth() * 3, GameManager.screenHeight - BitmapCacher.controller_right.getHeight() * 250 / 100);
        controller_up = new Point(BitmapCacher.controller_left.getWidth() * 250 / 100,
                GameManager.screenHeight - BitmapCacher.controller_left.getHeight() * 2);
        controller_down = new Point(BitmapCacher.controller_left.getWidth() * 250 / 100,
                GameManager.screenHeight - BitmapCacher.controller_left.getHeight());
        controller_punch = new Point(GameManager.screenWidth - BitmapCacher.controller_punch.getWidth() * 2,
                GameManager.screenHeight - BitmapCacher.controller_punch.getWidth() * 2);

        variablePosOfJoystick = new Point((GameManager.screenWidth * 20) / 100, (GameManager.screenHeight * 60) / 100);
        controllerJoystickRing = new Point(variablePosOfJoystick.x, variablePosOfJoystick.y);
        controllerJoystickInnerDefaultPos = new Point(variablePosOfJoystick.x, variablePosOfJoystick.y);
        controllerJoystickInnerPart = new Point(variablePosOfJoystick.x, variablePosOfJoystick.y);
        controllerWRTPlayer = new Point(0, 0);
        joystickOuterRadius = BitmapCacher.controller_joystick_ring.getWidth() / 2;

    }

    public void gameOver()
    {
        if (gameMusic != null)
        {
            gameMusic.stop();
            gameMusic.unload();
        }
        if (Door.doorOpen)
        {
            stopAlarm();
        }
        try
        {
            gameOverMusic = new Music(100, "/music/police_siren", 1);
            if (gameOverMusic != null && Game.isSoundEnabled)
            {
                gameOverMusic.play();
            }
        }
        catch (Exception ex)
        {
            ex.printStackTrace();
        }
        startTime = PlatformService.currentTimeMillis();
        currentScreen = Constants.SCREEN_GAMEOVER;
    }

    public void update(float deltaTime)
    {
        try
        {
            if (currentScreen == Constants.SCREEN_LOADING_SCREEN)
            {
                if (!GameManager.isLoadingScreen)
                {
                    GameManager.isLoadingScreen = true;
                    return;
                }
                loadGame();
            }
            else if (currentScreen == Constants.SCREEN_MAP_VIEW_MODE)
            {
                for (int i = 0; i < GameObjectManager.gameObjectList.size(); i++)
                {
                    GameObject go = (GameObject) GameObjectManager.gameObjectList.elementAt(i);
                    //--------------------------------------updating Door for timer purpose-----------------------------------------//
                    if (go.ID == Constants.GAMEOBJECT_DOOR)
                    {
                        go.updateObject();
                    }
                    //--------------------------------------------------------------------------------------------------------------//
                }
                if (leftMapUpdate)
                {
                    if (map.startX > 0)
                    {
                        map.MoveMapX(-MAP_SCROLL_SPEED);
                    }

                }
                else if (rightMapUpdate)
                {
                    if (map.startX + GameManager.screenWidth < map.maxX)
                    {
                        map.MoveMapX(MAP_SCROLL_SPEED);
                    }
                }
                else if (upMapUpdate)
                {
                    if (map.startY > 0)
                    {
                        map.MoveMapY(-MAP_SCROLL_SPEED);
                    }
                }
                else if (downMapUpdate)
                {
                    if (map.startY + GameManager.screenHeight < map.maxY)
                    {
                        map.MoveMapY(MAP_SCROLL_SPEED);
                    }
                }
            }
            else if (currentScreen == Constants.SCREEN_LOADING_COMPLETED_SCREEN)
            {
                if (!blinking)
                {
                    startTime = PlatformService.currentTimeMillis();
                    blinking = true;
                }
            }
            else if (currentScreen == Constants.SCREEN_RUNNING)
            {
                player.updateObject();
                gom.updateObjects();
                tapControllerMessage.update();          //---------------------when tap controller setting selected this message queue
            }
            else if (currentScreen == Constants.SCREEN_GAMEOVER)
            {
                if (!PlatformService.isTouchSupported())
                {
                    if (leftMouseupdate)
                    {
                        mousePointerPosition.x -= MOUSE_SPEED;
                        if (mousePointerPosition.x < 0)
                        {
                            mousePointerPosition.x = 0;
                        }
                    }
                    else if (rightMouseUpdate)
                    {
                        mousePointerPosition.x += MOUSE_SPEED;
                        if (mousePointerPosition.x >= GameManager.screenWidth - mousePointer.getWidth())
                        {
                            mousePointerPosition.x = GameManager.screenWidth - mousePointer.getWidth();
                        }
                    }
                    else if (upMouseUpdate)
                    {
                        mousePointerPosition.y -= MOUSE_SPEED;
                        if (mousePointerPosition.y < 0)
                        {
                            mousePointerPosition.y = 0;
                        }
                    }
                    else if (downMouseUpdate)
                    {
                        mousePointerPosition.y += MOUSE_SPEED;
                        if (mousePointerPosition.y >= GameManager.screenHeight - mousePointer.getHeight())
                        {
                            mousePointerPosition.y = GameManager.screenHeight - mousePointer.getHeight();
                        }
                    }
                }

            }
            //--------------------------------------updating Door for timer purpose-----------------------------------------//
            else if (currentScreen == Constants.SCREEN_BUTTON_PLACEMENT_SCREEN
                    || currentScreen == Constants.SCREEN_CONTROLLER_SETTING
                    || currentScreen == Constants.SCREEN_CONTROLLER_WRT_PLAYER_PLACEMENT
                    || currentScreen == Constants.SCREEN_JOYSTICK_PLACEMENT_SCREEN)
            {
                for (int i = 0; i < GameObjectManager.gameObjectList.size(); i++)
                {
                    GameObject go = (GameObject) GameObjectManager.gameObjectList.elementAt(i);
                    if (go.ID == Constants.GAMEOBJECT_DOOR)
                    {
                        go.updateObject();
                    }
                }
            }
            //-----------------------------------------------------------------------------------------------------------------------------------------//
            else if (currentScreen == Constants.SCREEN_PAUSE)
            {
                if (leftMouseupdate)
                {
                    mousePointerPosition.x -= MOUSE_SPEED;
                    if (mousePointerPosition.x < 0)
                    {
                        mousePointerPosition.x = 0;
                    }
                }
                else if (rightMouseUpdate)
                {
                    mousePointerPosition.x += MOUSE_SPEED;
                    if (mousePointerPosition.x >= GameManager.screenWidth - mousePointer.getWidth())
                    {
                        mousePointerPosition.x = GameManager.screenWidth - mousePointer.getWidth();
                    }
                }
                else if (upMouseUpdate)
                {
                    mousePointerPosition.y -= MOUSE_SPEED;
                    if (mousePointerPosition.y < 0)
                    {
                        mousePointerPosition.y = 0;
                    }
                }
                else if (downMouseUpdate)
                {
                    mousePointerPosition.y += MOUSE_SPEED;
                    if (mousePointerPosition.y >= GameManager.screenHeight - mousePointer.getHeight())
                    {
                        mousePointerPosition.y = GameManager.screenHeight - mousePointer.getHeight();
                    }
                }
                for (int i = 0; i < GameObjectManager.gameObjectList.size(); i++)
                {
                    GameObject go = (GameObject) GameObjectManager.gameObjectList.elementAt(i);
                    //--------------------------------------updating Door bcoz timer is updating in Door class-----------------------------------------//
                    if (go.ID == Constants.GAMEOBJECT_DOOR)
                    {
                        go.updateObject();
                    }
                    //-------------------------------------------------------------------------------------//
                }
            }
            else if (currentScreen == Constants.SCREEN_RESTART_LOADING)
            {
                if (!GameManager.isLoadingScreen)
                {
                    GameManager.isLoadingScreen = true;
                    return;
                }
                restartGame();
            }
            else if (currentScreen == Constants.SCREEN_LEVEL_CLEAR)
            {
                if (!GameManager.isLoadingScreen)
                {
                    GameManager.isLoadingScreen = true;
                    return;
                }
                loadNextLevel();
            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }

    }

    public void paint(Graphics g)
    {
        try
        {
            if (currentScreen == Constants.SCREEN_LOADING_SCREEN)
            {
                Bitmap.drawBitmap(g, loadingScreenBackground, 0, 0);
                Bitmap.fillColor(g, GameManager.screenWidth * 20 / 100, GameManager.screenHeight * 96 / 100,
                        GameManager.screenWidth * 60 / 100, GameManager.screenHeight * 4 / 100, 255, 255, 255);
                Bitmap.fillColor(g, GameManager.screenWidth * 21 / 100, GameManager.screenHeight * 97 / 100, progressBarWidth,
                        GameManager.screenHeight * 2 / 100, 255, 0, 0);
                Game.gameFont.drawString("[TIP : " + (tipIndex + 1) + "]", g, GameManager.screenWidth / 2 - Game.gameFont.stringWidth("TIP:"),
                        GameManager.screenHeight * 10 / 100);
                for (int i = 0; i < tipstring[tipIndex].length; i++)
                {
                    Game.gameFont.drawString(tipstring[tipIndex][i], g, GameManager.screenWidth / 2 - Game.gameFont.stringWidth(tipstring[tipIndex][0]) / 2,
                            (i + 1) * (Game.gameFont.stringHeight() * 130 / 100) + (GameManager.screenHeight * 13 / 100));
                }
            }
            else if (currentScreen == Constants.SCREEN_MAP_VIEW_MODE)
            {
                Bitmap.fillColor(g, 0, 0, GameManager.screenWidth, GameManager.screenHeight, 135, 135, 135);
                map.DrawMap(g, GameManager.screenWidth, GameManager.screenHeight);

                for (int i = 0; i < GameObjectManager.gameObjectList.size(); i++)
                {
                    GameObject go = (GameObject) GameObjectManager.gameObjectList.elementAt(i);
                    go.paintObject(g);
                }

                Bitmap.drawBitmap(g, BitmapCacher.pauseScreenBg, 0, 0);

                if (!PlatformService.isTouchSupported())
                {
                    Game.gameFont.drawString("Back", g, 0, GameManager.screenHeight - Game.gameFont.stringHeight());
                    Game.gameFont.drawString("Use navigation keys", g, GameManager.screenWidth / 2 - Game.gameFont.stringWidth("Use navigation keys") / 2,
                            GameManager.screenHeight / 2 - Game.gameFont.stringHeight());
                    Game.gameFont.drawString("to view map", g, GameManager.screenWidth / 2 - Game.gameFont.stringWidth("Use navigation keys") / 2, GameManager.screenHeight / 2);
                }
                else
                {
                    Bitmap.drawBitmap(g, BitmapCacher.mapViewIcon, 0, 0);
                    Bitmap.drawBitmap(g, BitmapCacher.controller_left, 0, GameManager.screenHeight / 2 - BitmapCacher.controller_left.getHeight() / 2);
                    Bitmap.drawBitmap(g, BitmapCacher.controller_right, GameManager.screenWidth - BitmapCacher.controller_right.getWidth(),
                            GameManager.screenHeight / 2 - BitmapCacher.controller_left.getHeight() / 2);
                    Bitmap.drawBitmap(g, BitmapCacher.controller_up, GameManager.screenWidth / 2 - BitmapCacher.controller_up.getWidth() / 2, 0);
                    Bitmap.drawBitmap(g, BitmapCacher.controller_down, GameManager.screenWidth / 2 - BitmapCacher.controller_down.getWidth() / 2,
                            GameManager.screenHeight - BitmapCacher.controller_down.getHeight());
                }

            }
            else if (currentScreen == Constants.SCREEN_LOADING_COMPLETED_SCREEN)
            {
                String messege;
                if (PlatformService.isTouchSupported())
                {
                    messege = "Tap to continue ";
                }
                else
                {
                    messege = "Press any key to continue";
                }

                Bitmap.drawBitmap(g, loadingScreenBackground, 0, 0);
                if (blinking)
                {
                    currentTime = PlatformService.currentTimeMillis();
                    if (currentTime > startTime + BLINK_TIME)
                    {
                        Game.gameFont.drawString(messege, g, GameManager.screenWidth / 2 - Game.gameFont.stringWidth(messege) / 2, GameManager.screenHeight * 95 / 100 - Game.gameFont.stringHeight());
                    }
                    if (currentTime > startTime + BLINK_TIME * 3 / 2)
                    {
                        blinking = false;
                    }
                }
                Game.gameFont.drawString("[TIP : " + (tipIndex + 1) + "]", g, GameManager.screenWidth / 2 - Game.gameFont.stringWidth("TIP:"),
                        GameManager.screenHeight * 10 / 100);
                for (int i = 0; i < tipstring[tipIndex].length; i++)
                {
                    Game.gameFont.drawString(tipstring[tipIndex][i], g, GameManager.screenWidth / 2 - Game.gameFont.stringWidth(tipstring[tipIndex][0]) / 2,
                            (i + 1) * (Game.gameFont.stringHeight() * 130 / 100) + (GameManager.screenHeight * 13 / 100));
                }
            }
            else if (currentScreen == Constants.SCREEN_RUNNING)
            {
                Bitmap.fillColor(g, 0, 0, GameManager.screenWidth, GameManager.screenHeight, 135, 135, 135);
                map.DrawMap(g, GameManager.screenWidth, GameManager.screenHeight);
                gom.paintObjects(g);
                player.paintObject(g);

                if (PlatformService.isTouchSupported())
                {
                    switch (Game.currentControllerSetting)
                    {
                        case Constants.CONTROLLER_TYPE_SINGLE:
                            Bitmap.drawBitmap(g, BitmapCacher.controller_left, controller_left.x, controller_left.y);
                            Bitmap.drawBitmap(g, BitmapCacher.controller_right, controller_right.x, controller_right.y);
                            Bitmap.drawBitmap(g, BitmapCacher.controller_up, controller_up.x, controller_up.y);
                            Bitmap.drawBitmap(g, BitmapCacher.controller_down, controller_down.x, controller_down.y);
                            if (!isCopInRange)
                            {
                                Bitmap.drawBitmap(g, BitmapCacher.controller_punch, controller_punch.x, controller_punch.y);
                            }
                            else
                            {
                                //if player in range show red punch buton
                                Bitmap.drawBitmap(g, BitmapCacher.controller_punch_red, controller_punch.x, controller_punch.y);
                            }
                            break;

                        case Constants.CONTROLLER_TYPE_JOYSTICK:
                            Bitmap.drawBitmap(g, BitmapCacher.controller_joystick_ring, controllerJoystickRing.x - BitmapCacher.controller_joystick_ring.getWidth() / 2,
                                    controllerJoystickRing.y - BitmapCacher.controller_joystick_ring.getWidth() / 2);
                            Bitmap.drawBitmap(g, BitmapCacher.controller_joystick_innerPart, controllerJoystickInnerPart.x - BitmapCacher.controller_joystick_innerPart.getWidth() / 2,
                                    controllerJoystickInnerPart.y - BitmapCacher.controller_joystick_innerPart.getHeight() / 2);
                            if (!isCopInRange)
                            {
                                Bitmap.drawBitmap(g, BitmapCacher.controller_punch, controller_punch.x, controller_punch.y);
                            }
                            else
                            {
                                //if player in range show red punch buton
                                Bitmap.drawBitmap(g, BitmapCacher.controller_punch_red, controller_punch.x, controller_punch.y);
                            }

                            break;
                        case Constants.CONTROLLER_TYPE_WRT_PLAYER:
                            if (!isCopInRange)
                            {
                                Bitmap.drawBitmap(g, BitmapCacher.controller_punch, controller_punch.x, controller_punch.y);
                            }
                            else
                            {
                                //if player in range show red punch buton
                                Bitmap.drawBitmap(g, BitmapCacher.controller_punch_red, controller_punch.x, controller_punch.y);
                            }
                            break;
                    }
                    if (controller_pressed)
                    {
                        Bitmap.drawBitmap(g, BitmapCacher.controller_touched, controller_touched.x, controller_touched.y);
                    }
                    Bitmap.drawBitmap(g, BitmapCacher.mapViewIcon, mapViewModeIconPoint.x, mapViewModeIconPoint.y);
                    Bitmap.drawBitmap(g, BitmapCacher.pause_button, pauseButtonPosition.x, pauseButtonPosition.y);
                    tapControllerMessage.paint(g);
                }
                else
                {
                    Game.gameFont.drawString("Viewmap", g, 0, GameManager.screenHeight - Game.gameFont.stringHeight());
                    Game.gameFont.drawString("Pause", g, GameManager.screenWidth - Game.gameFont.stringWidth("Pause"), GameManager.screenHeight - Game.gameFont.stringHeight());
                }

                if (currentLevel < 43)//----------------------------after level 42 maps are big so it will take lot of time to complete the map
                {                                                               //--------------thats y the alarm is not implemented after 42 
                    if (Door.doorOpen)
                    {
                        time = (timeLimit - (Door.currentTimeElapsed - Door.alarmStartTime)) / 1000;
                        if (time > ((timeLimit / 1000) - 5))
                        {
                            String msg = "you've only " + timeLimit / 1000 + "s to exit";
                            Game.gameFont.drawString(msg, g, GameManager.screenWidth / 2 - Game.gameFont.stringWidth(msg) / 2, GameManager.screenHeight / 2 - Game.gameFont.stringHeight() / 2);
                        }
                        int ypos, xpos;
                        if (PlatformService.isTouchSupported())
                        {
                            xpos = GameManager.screenWidth / 2 - Game.gameFont.stringWidth("Time left: (s)") / 2;
                            ypos = GameManager.screenHeight - Game.gameFont.stringHeight();
                        }
                        else
                        {
                            xpos = 0;
                            ypos = 0;
                        }

                        Game.gameFont.drawString("Time left:" + time + "(s)", g, xpos, ypos);
                        if (time <= 0)
                        {
                            gameOver();
                        }
                    }
                }
                int xpos;
                if (PlatformService.isTouchSupported())
                {
                    xpos = GameManager.screenWidth / 2 - Game.gameFont.stringWidth("Level " + LevelSelectView.levelStartCount) / 2;
                }
                else
                {
                    xpos = GameManager.screenWidth - Game.gameFont.stringWidth("Level " + LevelSelectView.levelStartCount);
                }
                Game.gameFont.drawString("Level " + currentLevel, g, xpos, 0);
            }
            else if (currentScreen == Constants.SCREEN_FREEDOM)
            {
                Bitmap.drawBitmap(g, freedomBackground, 0, 0);
                if (PlatformService.isTouchSupported())
                {
                    Bitmap.drawBitmap(g, backButton, backButtonPosition.x, backButtonPosition.y);
                }
                else
                {
                    Game.gameFont.drawString("Back", g, 0, GameManager.screenHeight - Game.gameFont.stringHeight());
                }
            }
            else if (currentScreen == Constants.SCREEN_GAMEOVER)
            {
                currentTime = PlatformService.currentTimeMillis();
                if (currentTime - startTime > 1000)
                {
                    Bitmap.drawBitmap(g, BitmapCacher.background, 0, 0);
                    Bitmap.drawBitmap(g, BitmapCacher.youAreFailed, GameManager.screenWidth / 2 - BitmapCacher.youAreFailed.getWidth() / 2, GameManager.screenHeight * 5 / 100);
                    Bitmap.drawBitmap(g, BitmapCacher.handCuffed, GameManager.screenWidth / 2 - BitmapCacher.handCuffed.getWidth() / 2, GameManager.screenHeight * 15 / 100);
                    Bitmap.drawBitmap(g, BitmapCacher.replay, restartPositionGameOverScreen.x, restartPositionGameOverScreen.y);
                    Bitmap.drawBitmap(g, BitmapCacher.quit, quitPositionGameOverScreen.x, quitPositionGameOverScreen.y);
                    Bitmap.drawBitmap(g, BitmapCacher.bars, GameManager.screenWidth / 2 - BitmapCacher.bars.getWidth() / 2, GameManager.screenHeight / 2 - BitmapCacher.bars.getHeight() / 2);
                    if (!PlatformService.isTouchSupported())
                    {
                        Bitmap.drawBitmap(g, mousePointer, mousePointerPosition.x, mousePointerPosition.y);
                    }
                }
                else
                {
                    Bitmap.fillColor(g, 0, 0, GameManager.screenWidth, GameManager.screenHeight, 135, 135, 135);
                    map.DrawMap(g, GameManager.screenWidth, GameManager.screenHeight);
                    player.paintObject(g);
                    gom.paintObjects(g);
                }
            }
            else if (currentScreen == Constants.SCREEN_PAUSE)
            {
                Bitmap.fillColor(g, 0, 0, GameManager.screenWidth, GameManager.screenHeight, 135, 135, 135);
                map.DrawMap(g, GameManager.screenWidth, GameManager.screenHeight);
                gom.paintObjects(g);
                player.paintObject(g);
                Bitmap.drawBitmap(g, BitmapCacher.pauseScreenBg, 0, 0);
                Bitmap.drawBitmap(g, BitmapCacher.bars, GameManager.screenWidth / 2 - BitmapCacher.bars.getWidth() / 2, GameManager.screenHeight / 2 - BitmapCacher.bars.getHeight() / 2);
                Bitmap.drawBitmap(g, BitmapCacher.resume, resumeButtonPositionPauseScreen.x, resumeButtonPositionPauseScreen.y);
                Bitmap.drawBitmap(g, BitmapCacher.replay, restartButtonPositionPauseScreen.x, restartButtonPositionPauseScreen.y);
                Bitmap.drawBitmap(g, BitmapCacher.quit, quitButtonPositionPauseScreen.x, quitButtonPositionPauseScreen.y);
                if (!PlatformService.isTouchSupported())
                {
                    Bitmap.drawBitmap(g, mousePointer, mousePointerPosition.x, mousePointerPosition.y);
                }
                else
                {
                    Bitmap.drawBitmap(g, BitmapCacher.pauseControlsSetting, controlsButtonPositionPauseMenu.x, controlsButtonPositionPauseMenu.y);
                }
            }
            else if (currentScreen == Constants.SCREEN_RESTART_LOADING)
            {
                Bitmap.drawBitmap(g, BitmapCacher.pauseScreenBg, 0, 0);
                Game.gameFont.drawString("Loading......", g, GameManager.screenWidth - Game.gameFont.stringWidth("Loading......"), GameManager.screenHeight - Game.gameFont.stringHeight() * 2);
            }
            else if (currentScreen == Constants.SCREEN_LEVEL_CLEAR)
            {
                Bitmap.drawBitmap(g, loadingScreenBackground, 0, 0);
                Game.gameFont.drawString("Loading......", g, GameManager.screenWidth / 2 - Game.gameFont.stringWidth("Loading......") / 2, GameManager.screenHeight / 2 - Game.gameFont.stringHeight());
            }
            else if (currentScreen == Constants.SCREEN_CONTROLLER_SETTING)
            {
                Bitmap.drawBitmap(g, loadingScreenBackground, 0, 0);
                Bitmap.drawBitmap(g, BitmapCacher.controller_setting_change_controller, GameManager.screenWidth * 30 / 100, GameManager.screenHeight * 40 / 100);
                Bitmap.drawBitmap(g, BitmapCacher.controller_setting_reposition, GameManager.screenWidth * 60 / 100, GameManager.screenHeight * 40 / 100);
                Bitmap.drawBitmap(g, BitmapCacher.yesCheck, 0, GameManager.screenHeight - BitmapCacher.yesCheck.getHeight());
                String msg = "";
                if (Game.currentControllerSetting == 1)
                {
                    msg = "Single Controller";
                }
                else if (Game.currentControllerSetting == 2)
                {
                    msg = "Joystick Controller";
                }
                else if (Game.currentControllerSetting == 3)
                {
                    msg = "Tap Controller";
                }
                Game.gameFont.drawString("Controller Type:", g, GameManager.screenWidth / 2 - Game.gameFont.stringWidth("Controller Type:") / 2,
                        GameManager.screenHeight * 70 / 100);
                Game.gameFont.drawString(msg, g, GameManager.screenWidth / 2 - Game.gameFont.stringWidth(msg) / 2,
                        GameManager.screenHeight * 80 / 100);
            }
            else if (currentScreen == Constants.SCREEN_JOYSTICK_PLACEMENT_SCREEN)
            {
                Bitmap.fillColor(g, 0, 0, GameManager.screenWidth, GameManager.screenHeight, 99, 99, 99);
                map.DrawMap(g, GameManager.screenWidth, GameManager.screenHeight);
                gom.paintObjects(g);
                player.paintObject(g);
                Bitmap.drawBitmap(g, BitmapCacher.pauseScreenBg, 0, 0);
                Bitmap.drawBitmap(g, BitmapCacher.controller_joystick_ring, controllerJoystickRing.x - BitmapCacher.controller_joystick_ring.getWidth() / 2,
                        controllerJoystickRing.y - BitmapCacher.controller_joystick_ring.getWidth() / 2);
                Bitmap.drawBitmap(g, BitmapCacher.controller_joystick_innerPart, controllerJoystickInnerPart.x - BitmapCacher.controller_joystick_innerPart.getWidth() / 2,
                        controllerJoystickInnerPart.y - BitmapCacher.controller_joystick_innerPart.getHeight() / 2);
                Bitmap.drawBitmap(g, BitmapCacher.controller_punch, controller_punch.x, controller_punch.y);
                Bitmap.drawBitmap(g, BitmapCacher.yesCheck, 0, GameManager.screenHeight - BitmapCacher.yesCheck.getHeight());
                Game.gameFont.drawString("Drag joystick anywhere to position it", g, GameManager.screenWidth / 2
                        - Game.gameFont.stringWidth("Drag joystick anywhere to position it") / 2, GameManager.screenHeight * 30 / 100);
            }
            else if (currentScreen == Constants.SCREEN_BUTTON_PLACEMENT_SCREEN)
            {
                Bitmap.fillColor(g, 0, 0, GameManager.screenWidth, GameManager.screenHeight, 99, 99, 99);
                map.DrawMap(g, GameManager.screenWidth, GameManager.screenHeight);
                gom.paintObjects(g);
                player.paintObject(g);
                Bitmap.drawBitmap(g, BitmapCacher.pauseScreenBg, 0, 0);
                Bitmap.drawBitmap(g, BitmapCacher.controller_left, controller_left.x, controller_left.y);
                Bitmap.drawBitmap(g, BitmapCacher.controller_right, controller_right.x, controller_right.y);
                Bitmap.drawBitmap(g, BitmapCacher.controller_up, controller_up.x, controller_up.y);
                Bitmap.drawBitmap(g, BitmapCacher.controller_down, controller_down.x, controller_down.y);
                Bitmap.drawBitmap(g, BitmapCacher.controller_punch, controller_punch.x, controller_punch.y);
                Bitmap.drawBitmap(g, BitmapCacher.yesCheck, 0, GameManager.screenHeight - BitmapCacher.yesCheck.getHeight());
                Game.gameFont.drawString("Drag buttons anywhere to position them", g, GameManager.screenWidth / 2
                        - Game.gameFont.stringWidth("Drag joystick anywhere to position it") / 2, GameManager.screenHeight * 30 / 100);
            }
            else if (currentScreen == Constants.SCREEN_CONTROLLER_WRT_PLAYER_PLACEMENT)
            {
                Bitmap.fillColor(g, 0, 0, GameManager.screenWidth, GameManager.screenHeight, 99, 99, 99);
                map.DrawMap(g, GameManager.screenWidth, GameManager.screenHeight);
                gom.paintObjects(g);
                player.paintObject(g);
                Bitmap.drawBitmap(g, BitmapCacher.pauseScreenBg, 0, 0);
                Bitmap.drawBitmap(g, BitmapCacher.controller_punch, controller_punch.x, controller_punch.y);
                Bitmap.drawBitmap(g, BitmapCacher.yesCheck, 0, GameManager.screenHeight - BitmapCacher.yesCheck.getHeight());
                Game.gameFont.drawString("Drag to reposition punch button", g, GameManager.screenWidth / 2
                        - Game.gameFont.stringWidth("Drag to reposition punch button") / 2, GameManager.screenHeight * 30 / 100);
            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }

    }

    public void keyPressed(int keyID)
    {
        if (currentScreen == Constants.SCREEN_RUNNING)
        {
            if (keyID == Constants.LEFTKEY)
            {
                GamePlayView.instance.player.moveLeft();
            }
            else if (keyID == Constants.RIGHTKEY)
            {
                GamePlayView.instance.player.moveRight();
            }
            else if (keyID == Constants.UPKEY)
            {
                GamePlayView.instance.player.moveUp();
            }
            else if (keyID == Constants.DOWNKEY)
            {
                GamePlayView.instance.player.moveDown();
            }
            else if (keyID == Constants.KEYNUM_3 || keyID == Constants.MIDDLESOFT_KEY)
            {
                GamePlayView.instance.player.punch();
            }
            else if (keyID == Constants.RIGHTSOFT_KEY)
            {
                if (Door.doorOpen)
                {
                    Door.tempTime = Door.currentTimeElapsed;
                }
                pauseMusicAndSound();
                currentScreen = Constants.SCREEN_PAUSE;
            }
            else if (keyID == Constants.LEFTSOFT_KEY)
            {
                playerPosition = player.position;
                tempStartX = map.startX;
                tempStartY = map.startY;
                if (Door.doorOpen)
                {
                    Door.tempTime = Door.currentTimeElapsed;
                }
                currentScreen = Constants.SCREEN_MAP_VIEW_MODE;
            }
        }
        else if (currentScreen == Constants.SCREEN_PAUSE || currentScreen == Constants.SCREEN_GAMEOVER)
        {
            if (keyID == Constants.KEYNUM_4 || keyID == Constants.LEFTKEY)
            {
                leftMouseupdate = true;
            }
            else if (keyID == Constants.KEYNUM_6 || keyID == Constants.RIGHTKEY)
            {
                rightMouseUpdate = true;
            }
            else if (keyID == Constants.KEYNUM_2 || keyID == Constants.UPKEY)
            {
                upMouseUpdate = true;
            }
            else if (keyID == Constants.KEYNUM_8 || keyID == Constants.DOWNKEY)
            {
                downMouseUpdate = true;
            }
            else if (keyID == Constants.KEYNUM_9 || keyID == Constants.MIDDLESOFT_KEY)
            {
                pointerPressed(-1, mousePointerPosition.x, mousePointerPosition.y);
            }
        }
        else if (currentScreen == Constants.SCREEN_MAP_VIEW_MODE)
        {
            if (keyID == Constants.LEFTKEY)
            {
                leftMapUpdate = true;
            }
            else if (keyID == Constants.RIGHTKEY)
            {
                rightMapUpdate = true;
            }
            else if (keyID == Constants.UPKEY)
            {
                upMapUpdate = true;
            }
            else if (keyID == Constants.DOWNKEY)
            {
                downMapUpdate = true;
            }
            else if (keyID == Constants.LEFTSOFT_KEY)
            {
                player.position = playerPosition;
                int offsetX = map.startX - tempStartX;
                int offsetY = map.startY - tempStartY;

                map.MoveMapX(-offsetX);
                map.MoveMapY(-offsetY);

                if (Door.doorOpen)
                {
                    timeLimit += (Door.currentTimeElapsed - Door.tempTime);
                    time += (Door.currentTimeElapsed - Door.tempTime);
                }
                currentScreen = Constants.SCREEN_RUNNING;
            }
        }
        else if (currentScreen == Constants.SCREEN_LOADING_COMPLETED_SCREEN)
        {
            currentScreen = Constants.SCREEN_RUNNING;
        }
        else if (currentScreen == Constants.SCREEN_FREEDOM)
        {
            if (keyID == Constants.LEFTSOFT_KEY)
            {
                Game.changeView(Constants.VIEW_MENUVIEW);
            }
            else if (keyID == Constants.RIGHTSOFT_KEY)
            {
                Game.changeView(Constants.VIEW_ABOUTVIEW);
            }
        }
    }

    public void keyReleased(int keyID)
    {
        if (currentScreen == Constants.SCREEN_RUNNING)
        {
            Player.moveMapSpeed.x = 0;
            Player.moveMapSpeed.y = 0;

            controller_pressed = false;
            if (keyID == Constants.LEFTKEY)
            {
                if (GamePlayView.instance.player.imageSet.currentState == Constants.PLAYER_STATE_LEFT_WALK)
                {
                    GamePlayView.instance.player.imageSet.setState(Constants.PLAYER_STATE_IDLE_LEFT, false, 3);
                }
                moveLeft = false;
            }
            else if (keyID == Constants.RIGHTKEY)
            {
                if (GamePlayView.instance.player.imageSet.currentState == Constants.PLAYER_STATE_RIGHT_WALK)
                {
                    GamePlayView.instance.player.imageSet.setState(Constants.PLAYER_STATE_IDLE_RIGHT, false, 3);
                }
                moveRight = false;
            }
            else if (keyID == Constants.UPKEY)
            {
                if (GamePlayView.instance.player.imageSet.currentState == Constants.PLAYER_STATE_UP_WALK)
                {
                    GamePlayView.instance.player.imageSet.setState(Constants.PLAYER_STATE_IDLE_UP, false, 3);
                }
                moveUp = false;
            }
            else if (keyID == Constants.DOWNKEY)
            {
                if (GamePlayView.instance.player.imageSet.currentState == Constants.PLAYER_STATE_DOWN_WALK)
                {
                    GamePlayView.instance.player.imageSet.setState(Constants.PLAYER_STATE_IDLE_DOWN, false, 3);

                }
                moveDown = false;
            }

        }

        else if (currentScreen == Constants.SCREEN_GAMEOVER || currentScreen == Constants.SCREEN_PAUSE)
        {
            if (keyID == Constants.KEYNUM_4 || keyID == Constants.LEFTKEY)
            {
                leftMouseupdate = false;
            }
            else if (keyID == Constants.KEYNUM_6 || keyID == Constants.RIGHTKEY)
            {
                rightMouseUpdate = false;
            }
            else if (keyID == Constants.KEYNUM_2 || keyID == Constants.UPKEY)
            {
                upMouseUpdate = false;
            }
            else if (keyID == Constants.KEYNUM_8 || keyID == Constants.DOWNKEY)
            {
                downMouseUpdate = false;
            }
        }
        else if (currentScreen == Constants.SCREEN_MAP_VIEW_MODE)
        {
            leftMapUpdate = false;
            rightMapUpdate = false;
            upMapUpdate = false;
            downMapUpdate = false;

            Player.moveMapSpeed.x = 0;
            Player.moveMapSpeed.y = 0;

        }

    }

    public void keyRepeated(int keyID)
    {
    }

    public void pointerPressed(int pointerID, int x, int y)
    {
        if (currentScreen == Constants.SCREEN_RUNNING)
        {
            if (x > GameManager.screenWidth - BitmapCacher.pause_button.getWidth() && x < GameManager.screenWidth
                    && y > 0 && y < BitmapCacher.pause_button.getHeight())
            {
                if (Game.isSoundEnabled && Game.clickSound != null)
                {
                    Game.clickSound.play();
                }
                if (Door.doorOpen)
                {
                    Door.tempTime = Door.currentTimeElapsed;
                }
                pauseMusicAndSound();
                currentScreen = Constants.SCREEN_PAUSE;
            }
            else if (x > mapViewModeIconPoint.x && x < mapViewModeIconPoint.x + BitmapCacher.mapViewIcon.getWidth()
                    && y > mapViewModeIconPoint.y && y < mapViewModeIconPoint.y + BitmapCacher.mapViewIcon.getHeight())
            {
                if (Game.isSoundEnabled && Game.clickSound != null)
                {
                    Game.clickSound.play();
                }
                playerPosition.x = player.position.x;
                playerPosition.y = player.position.y;
                tempStartX = map.startX;
                tempStartY = map.startY;
                if (Door.doorOpen)
                {
                    Door.tempTime = Door.currentTimeElapsed;
                }
                currentScreen = Constants.SCREEN_MAP_VIEW_MODE;
            }
            else if (Game.currentControllerSetting == Constants.CONTROLLER_TYPE_SINGLE)
            {
                implementSingleControllerKeyPressed(pointerID, x, y);
            }
            else if (Game.currentControllerSetting == Constants.CONTROLLER_TYPE_JOYSTICK)
            {

                //--------------------Punch button----------------------//
                if (x > controller_punch.x - BitmapCacher.controller_punch.getWidth() / 2
                        && x < controller_punch.x + BitmapCacher.controller_punch.getWidth() * 3 / 2
                        && y > controller_punch.y - BitmapCacher.controller_punch.getWidth() / 2
                        && y < controller_punch.y + BitmapCacher.controller_punch.getHeight() * 3 / 2)
                {
                    punchTouchPointerID = pointerID;
                    controller_pressed = true;
                    controller_touched = controller_punch;
                    GamePlayView.instance.player.punch();
                }
                else
                {
                }
            }
            else if (Game.currentControllerSetting == Constants.CONTROLLER_TYPE_WRT_PLAYER)
            {
                controllerWRTPlayer.x = x;
                controllerWRTPlayer.y = y;
                implementTouchWRTPlayerController(pointerID, x, y);
            }
        }
        else if (currentScreen == Constants.SCREEN_MAP_VIEW_MODE)
        {
            if (x > 0 && x < BitmapCacher.mapViewIcon.getWidth()
                    && y > 0 && y < BitmapCacher.mapViewIcon.getHeight())
            {
                player.position.x = playerPosition.x;
                player.position.y = playerPosition.y;
                int offsetX = map.startX - tempStartX;
                int offsetY = map.startY - tempStartY;

                map.MoveMapX(-offsetX);
                map.MoveMapY(-offsetY);
                if (Door.doorOpen)
                {
                    timeLimit += (Door.currentTimeElapsed - Door.tempTime);
                    time += (Door.currentTimeElapsed - Door.tempTime);
                }
                currentScreen = Constants.SCREEN_RUNNING;
            }
            else if (x > 0 && x < GameManager.screenWidth * 30 / 100
                    && y > GameManager.screenHeight * 30 / 100 && y < GameManager.screenHeight * 70 / 100)
            {
                keyPressed(Constants.LEFTKEY);
            }
            else if (x > GameManager.screenWidth * 70 / 100 && x < GameManager.screenWidth
                    && y > GameManager.screenHeight * 30 / 100 && y < GameManager.screenHeight * 70 / 100)
            {
                keyPressed(Constants.RIGHTKEY);
            }
            else if (x > GameManager.screenWidth * 40 / 100 && x < GameManager.screenWidth * 60 / 100)
            {
                if (y > 0 && y < GameManager.screenHeight * 30 / 100)
                {
                    keyPressed(Constants.UPKEY);
                }
                else if (y > GameManager.screenHeight * 70 / 100 && y < GameManager.screenHeight)
                {
                    keyPressed(Constants.DOWNKEY);
                }
            }
        }
        else if (currentScreen == Constants.SCREEN_GAMEOVER)
        {
            if (x > restartPositionGameOverScreen.x && x < restartPositionGameOverScreen.x + BitmapCacher.resume.getWidth()
                    && y > restartPositionGameOverScreen.y && y < restartPositionGameOverScreen.y + BitmapCacher.resume.getHeight())
            {
                if (Game.isSoundEnabled && Game.clickSound != null)
                {
                    Game.clickSound.play();
                }
                if (gameOverMusic != null && Game.isSoundEnabled)
                {
                    gameOverMusic.stop();
                    gameOverMusic.unload();
                }
                currentScreen = Constants.SCREEN_RESTART_LOADING;
            }
            else if (x > quitPositionGameOverScreen.x && x < quitPositionGameOverScreen.x + BitmapCacher.quit.getWidth()
                    && y > quitPositionGameOverScreen.y && y < quitPositionGameOverScreen.y + BitmapCacher.quit.getHeight())
            {
                if (Game.isSoundEnabled && Game.clickSound != null)
                {
                    Game.clickSound.play();
                }
                if (gameOverMusic != null && Game.isSoundEnabled)
                {
                    gameOverMusic.stop();
                    gameOverMusic.unload();
                }
                Game.changeView(Constants.VIEW_MENUVIEW);
            }
        }
        else if (currentScreen == Constants.SCREEN_LOADING_COMPLETED_SCREEN)
        {
            currentScreen = Constants.SCREEN_RUNNING;
        }
        else if (currentScreen == Constants.SCREEN_FREEDOM)
        {
            if (x > backButtonPosition.x && x < backButtonPosition.x + backButton.getWidth() * 2
                    && y > backButtonPosition.y && y < GameManager.screenHeight)
            {
                Game.changeView(Constants.VIEW_MENUVIEW);
            }
            else
            {
                Game.changeView(Constants.VIEW_ABOUTVIEW);
            }
        }
        else if (currentScreen == Constants.SCREEN_PAUSE)
        {
            if (x > resumeButtonPositionPauseScreen.x && x < resumeButtonPositionPauseScreen.x + BitmapCacher.resume.getWidth()
                    && y > resumeButtonPositionPauseScreen.y && y < resumeButtonPositionPauseScreen.y + BitmapCacher.resume.getHeight())
            {
                if (Game.isSoundEnabled && Game.clickSound != null)
                {
                    Game.clickSound.play();
                }
                if (Door.doorOpen)
                {
                    timeLimit += (Door.currentTimeElapsed - Door.tempTime);
                    time += (Door.currentTimeElapsed - Door.tempTime);
                }
                resumeMusicAndSound();
                currentScreen = Constants.SCREEN_RUNNING;
            }
            else if (x > quitButtonPositionPauseScreen.x && x < quitButtonPositionPauseScreen.x + BitmapCacher.quit.getWidth()
                    && y > quitButtonPositionPauseScreen.y && y < quitButtonPositionPauseScreen.y + BitmapCacher.quit.getHeight())
            {
                if (Game.isSoundEnabled && Game.clickSound != null)
                {
                    Game.clickSound.play();
                }
                Game.changeView(Constants.VIEW_MENUVIEW);
            }
            else if (x > restartButtonPositionPauseScreen.x && x < restartButtonPositionPauseScreen.x + BitmapCacher.replay.getWidth()
                    && y > restartButtonPositionPauseScreen.y && y < restartButtonPositionPauseScreen.y + BitmapCacher.replay.getHeight())
            {
                if (Game.isSoundEnabled && Game.clickSound != null)
                {
                    Game.clickSound.play();
                }
                currentScreen = Constants.SCREEN_RESTART_LOADING;
            }
            else if (x > controlsButtonPositionPauseMenu.x && x < controlsButtonPositionPauseMenu.x + BitmapCacher.pauseControlsSetting.getWidth()
                    && y > controlsButtonPositionPauseMenu.y && y < controlsButtonPositionPauseMenu.y + BitmapCacher.pauseControlsSetting.getHeight())
            {
                if (PlatformService.isTouchSupported())
                {
                    if (Game.isSoundEnabled && Game.clickSound != null)
                    {
                        Game.clickSound.play();
                    }
                    currentScreen = Constants.SCREEN_CONTROLLER_SETTING;
                }
            }
        }
        else if (currentScreen == Constants.SCREEN_CONTROLLER_SETTING)
        {
            if (x > GameManager.screenWidth * 30 / 100 && x < GameManager.screenWidth * 30 / 100 + BitmapCacher.controller_setting_change_controller.getWidth()
                    && y > GameManager.screenHeight * 40 / 100 && y < GameManager.screenHeight * 40 / 100 + BitmapCacher.controller_setting_change_controller.getHeight())
            {
                if (Game.isSoundEnabled && Game.clickSound != null)
                {
                    Game.clickSound.play();
                }
                Game.currentControllerSetting++;
                if (Game.currentControllerSetting > 3)
                {
                    Game.currentControllerSetting = 1;
                }
            }
            else if (x > GameManager.screenWidth * 60 / 100 && x < GameManager.screenWidth * 60 / 100 + BitmapCacher.controller_setting_change_controller.getWidth()
                    && y > GameManager.screenHeight * 40 / 100 && y < GameManager.screenHeight * 40 / 100 + BitmapCacher.controller_setting_change_controller.getHeight())
            {
                if (Game.isSoundEnabled && Game.clickSound != null)
                {
                    Game.clickSound.play();
                }
                if (Game.currentControllerSetting == 1)
                {
                    PlatformService.disableMultitouch();
                    currentScreen = Constants.SCREEN_BUTTON_PLACEMENT_SCREEN;
                }
                else if (Game.currentControllerSetting == 2)
                {
                    PlatformService.disableMultitouch();
                    currentScreen = Constants.SCREEN_JOYSTICK_PLACEMENT_SCREEN;
                }
                else if (Game.currentControllerSetting == 3)
                {
                    PlatformService.disableMultitouch();
                    currentScreen = Constants.SCREEN_CONTROLLER_WRT_PLAYER_PLACEMENT;
                }
            }
            else if (x > 0 && x < BitmapCacher.yesCheck.getWidth() * 2
                    && y > GameManager.screenHeight - BitmapCacher.yesCheck.getHeight() * 150 / 100 && y < GameManager.screenHeight)
            {
                if (Game.isSoundEnabled && Game.clickSound != null)
                {
                    Game.clickSound.play();
                }
                if (Door.doorOpen)
                {
                    timeLimit += (Door.currentTimeElapsed - Door.tempTime);
                    time += (Door.currentTimeElapsed - Door.tempTime);
                }
                currentScreen = Constants.SCREEN_RUNNING;
            }
        }
        else if (currentScreen == Constants.SCREEN_BUTTON_PLACEMENT_SCREEN)
        {
            if (x > 0 && x < BitmapCacher.yesCheck.getWidth() * 2
                    && y > GameManager.screenHeight - BitmapCacher.yesCheck.getHeight() * 150 / 100 && y < GameManager.screenHeight)
            {
                if (Door.doorOpen)
                {
                    timeLimit += (Door.currentTimeElapsed - Door.tempTime);
                    time += (Door.currentTimeElapsed - Door.tempTime);
                }
                PlatformService.enableMultitouch();
                if (Game.isSoundEnabled && Game.clickSound != null)
                {
                    Game.clickSound.play();
                }
                currentScreen = Constants.SCREEN_RUNNING;
            }
            if (x > controller_left.x && x < controller_left.x + BitmapCacher.controller_left.getWidth()
                    && y > controller_left.y && y < controller_left.y + BitmapCacher.controller_left.getHeight())
            {
                moveButtonLeft = true;
            }
            else if (x > controller_right.x && x < controller_right.x + BitmapCacher.controller_right.getWidth()
                    && y > controller_right.y && y < controller_right.y + BitmapCacher.controller_right.getHeight())
            {
                moveButtonRight = true;
            }
            else if (x > controller_up.x && x < controller_up.x + BitmapCacher.controller_up.getWidth()
                    && y > controller_up.y && y < controller_up.y + BitmapCacher.controller_up.getHeight())
            {
                moveButtonUp = true;
            }
            else if (x > controller_down.x && x < controller_down.x + BitmapCacher.controller_down.getWidth()
                    && y > controller_down.y && y < controller_down.y + BitmapCacher.controller_down.getHeight())
            {
                moveButtonDown = true;
            }
            else if (x > controller_punch.x && x < controller_punch.x + BitmapCacher.controller_punch.getWidth()
                    && y > controller_punch.y && y < controller_punch.y + BitmapCacher.controller_punch.getHeight())
            {
                punchButtonDown = true;
            }
        }
        else if (currentScreen == Constants.SCREEN_JOYSTICK_PLACEMENT_SCREEN)
        {
            if (x > 0 && x < BitmapCacher.yesCheck.getWidth() * 2
                    && y > GameManager.screenHeight - BitmapCacher.yesCheck.getHeight() * 150 / 100 && y < GameManager.screenHeight)
            {
                if (Door.doorOpen)
                {
                    timeLimit += (Door.currentTimeElapsed - Door.tempTime);
                    time += (Door.currentTimeElapsed - Door.tempTime);
                }
                PlatformService.enableMultitouch();
                if (Game.isSoundEnabled && Game.clickSound != null)
                {
                    Game.clickSound.play();
                }
                currentScreen = Constants.SCREEN_RUNNING;
            }
            else if (x > controllerJoystickInnerDefaultPos.x - BitmapCacher.controller_joystick_ring.getWidth()
                    && x < controllerJoystickInnerDefaultPos.x + BitmapCacher.controller_joystick_ring.getWidth()
                    && y > controllerJoystickInnerDefaultPos.y - BitmapCacher.controller_joystick_ring.getWidth()
                    && y < controllerJoystickInnerDefaultPos.y + BitmapCacher.controller_joystick_ring.getHeight())
            {
                moveJoystickButton = true;
            }
            else if (x > controller_punch.x && x < controller_punch.x + BitmapCacher.controller_punch.getWidth()
                    && y > controller_punch.y && y < controller_punch.y + BitmapCacher.controller_punch.getHeight())
            {
                punchButtonDown = true;
            }
        }
        else if (currentScreen == Constants.SCREEN_CONTROLLER_WRT_PLAYER_PLACEMENT)
        {
            if (x > controller_punch.x && x < controller_punch.x + BitmapCacher.controller_punch.getWidth()
                    && y > controller_punch.y && y < controller_punch.y + BitmapCacher.controller_punch.getHeight())
            {
                punchButtonDown = true;
            }
            else if (x > 0 && x < BitmapCacher.yesCheck.getWidth() * 2
                    && y > GameManager.screenHeight - BitmapCacher.yesCheck.getHeight() * 150 / 100 && y < GameManager.screenHeight)
            {
                if (Door.doorOpen)
                {
                    timeLimit += (Door.currentTimeElapsed - Door.tempTime);
                    time += (Door.currentTimeElapsed - Door.tempTime);
                }
                PlatformService.enableMultitouch();
                if (Game.isSoundEnabled && Game.clickSound != null)
                {
                    Game.clickSound.play();
                }
                currentScreen = Constants.SCREEN_RUNNING;
            }
        }
        message += "p:" + pointerID + "(" + x + "," + y + ")";
    }

    public void pointerReleased(int pointerID, int x, int y)
    {
        if (currentScreen == Constants.SCREEN_RUNNING)
        {
            message = "";
            controllerJoystickInnerPart.x = controllerJoystickInnerDefaultPos.x;
            controllerJoystickInnerPart.y = controllerJoystickInnerDefaultPos.y;

            if (pointerID == navKeyTouchPointerID)
            {
                usingJoystick = false;
                keyReleased(Constants.RIGHTKEY);
                keyReleased(Constants.UPKEY);
                keyReleased(Constants.LEFTKEY);
                keyReleased(Constants.DOWNKEY);
                navKeyTouchPointerID = -999;
            }
            else
            {
                message = "first touch not released";
            }
            if (pointerID == punchTouchPointerID)
            {
                keyReleased(Constants.KEYNUM_3);
                keyReleased(Constants.MIDDLESOFT_KEY);
                punchTouchPointerID = -9991;
            }
            else
            {
            }
        }
        else if (currentScreen == Constants.SCREEN_MAP_VIEW_MODE)
        {
            leftMapUpdate = false;
            rightMapUpdate = false;
            upMapUpdate = false;
            downMapUpdate = false;
        }
        else if (currentScreen == Constants.SCREEN_BUTTON_PLACEMENT_SCREEN)
        {
            moveButtonLeft = false;
            moveButtonRight = false;
            moveButtonUp = false;
            moveButtonDown = false;
            punchButtonDown = false;
        }
        else if (currentScreen == Constants.SCREEN_JOYSTICK_PLACEMENT_SCREEN)
        {
            moveJoystickButton = false;
            punchButtonDown = false;
        }
        else if (currentScreen == Constants.SCREEN_CONTROLLER_WRT_PLAYER_PLACEMENT)
        {
            punchButtonDown = false;
        }
    }

    public void pointerDragged(int pointerID, int x, int y)
    {
        if (currentScreen == Constants.SCREEN_RUNNING)
        {
            if (Game.currentControllerSetting == Constants.CONTROLLER_TYPE_SINGLE)
            {
                if (x > controller_left.x - BitmapCacher.controller_left.getWidth()
                        && x < controller_left.x + BitmapCacher.controller_left.getWidth() * 200 / 100
                        && y > controller_left.y - BitmapCacher.controller_left.getWidth() * 50 / 100
                        && y < controller_left.y + BitmapCacher.controller_left.getHeight() * 150 / 100)
                {
                    navKeyTouchPointerID = pointerID;
                    controller_pressed = true;
                    controller_touched = controller_left;
                    GamePlayView.instance.player.moveLeft();
                }
                else if (x > controller_right.x - BitmapCacher.controller_left.getWidth()
                        && x < controller_right.x + BitmapCacher.controller_right.getWidth() * 200 / 100
                        && y > controller_right.y - BitmapCacher.controller_left.getWidth() * 50 / 100
                        && y < controller_right.y + BitmapCacher.controller_right.getHeight() * 150 / 100)
                {
                    navKeyTouchPointerID = pointerID;
                    controller_pressed = true;
                    controller_touched = controller_right;
                    GamePlayView.instance.player.moveRight();
                }
                else if (x > controller_up.x - BitmapCacher.controller_up.getWidth()
                        && x < controller_up.x + BitmapCacher.controller_up.getWidth() * 200 / 100
                        && y > controller_up.y - BitmapCacher.controller_up.getWidth()
                        && y < controller_up.y + BitmapCacher.controller_up.getHeight() * 150 / 100)
                {
                    navKeyTouchPointerID = pointerID;
                    controller_pressed = true;
                    controller_touched = controller_up;
                    GamePlayView.instance.player.moveUp();
                }
                else if (x > controller_down.x - BitmapCacher.controller_down.getWidth()
                        && x < controller_down.x + BitmapCacher.controller_down.getWidth() * 200 / 100
                        && y > controller_down.y - BitmapCacher.controller_down.getWidth()
                        && y < controller_down.y + BitmapCacher.controller_down.getHeight() * 150 / 100)
                {
                    navKeyTouchPointerID = pointerID;
                    controller_pressed = true;
                    controller_touched = controller_down;
                    GamePlayView.instance.player.moveDown();
                }

            }
            else if (Game.currentControllerSetting == Constants.CONTROLLER_TYPE_WRT_PLAYER)
            {
                controllerWRTPlayer.x = x;
                controllerWRTPlayer.y = y;
                implementTouchWRTPlayerController(pointerID, x, y);
            }

            else if (Game.currentControllerSetting == Constants.CONTROLLER_TYPE_JOYSTICK)
            {
                if (x > controller_punch.x - BitmapCacher.controller_punch.getWidth() / 2
                        && x < controller_punch.x + BitmapCacher.controller_punch.getWidth() * 3 / 2
                        && y > controller_punch.y - BitmapCacher.controller_punch.getWidth() / 2
                        && y < controller_punch.y + BitmapCacher.controller_punch.getHeight() * 3 / 2)
                {
                    Debug.print("you're dragging in punch button area...");
                }
                else
                {
                    navKeyTouchPointerID = pointerID;
                    if (calculateDistanceSquared(x, y, controllerJoystickInnerPart.x, controllerJoystickInnerPart.y)
                            <= (BitmapCacher.controller_joystick_innerPart.getWidth() * BitmapCacher.controller_joystick_innerPart.getWidth() * 2))
                    {
                        usingJoystick = true;
                    }
                    if (usingJoystick)
                    {
                        Vector2 normalizedVector = Touch.normalize(controllerJoystickInnerDefaultPos, new Point(x, y), joystickOuterRadius);

                        //-----if touch is outside circle, reset position of joystick, adding direction------//
                        controllerJoystickInnerPart.x = ((int) (normalizedVector.x * joystickOuterRadius)) + controllerJoystickInnerDefaultPos.x;
                        controllerJoystickInnerPart.y = -((int) (normalizedVector.y * joystickOuterRadius)) + controllerJoystickInnerDefaultPos.y;

                        int dir = Touch.getPlayerDirection(normalizedVector);

                        if (dir == Touch.DIRECTION_RIGHT)
                        {
                            GamePlayView.instance.player.moveRight();
                        }
                        else if (dir == Touch.DIRECTION_LEFT)
                        {
                            GamePlayView.instance.player.moveLeft();
                        }
                        else if (dir == Touch.DIRECTION_UP)
                        {
                            GamePlayView.instance.player.moveUp();
                        }
                        else if (dir == Touch.DIRECTION_DOWN)
                        {
                            GamePlayView.instance.player.moveDown();
                        }
                    }
                }
            }
        }
        else if (currentScreen == Constants.SCREEN_JOYSTICK_PLACEMENT_SCREEN)
        {
            if (x > GameManager.screenWidth * 15 / 100 && x < GameManager.screenWidth * 85 / 100)
            {
                if (moveJoystickButton)
                {
                    variablePosOfJoystick = new Point(x, y);
                    controllerJoystickRing.x = variablePosOfJoystick.x;
                    controllerJoystickInnerPart.x = variablePosOfJoystick.x;
                    controllerJoystickInnerDefaultPos.x = variablePosOfJoystick.x;
                }
                else if (punchButtonDown)
                {
                    controller_punch.x = x;
                }
            }
            if (y > GameManager.screenHeight * 25 / 100 && y < GameManager.screenHeight * 75 / 100)
            {
                if (moveJoystickButton)
                {
                    variablePosOfJoystick = new Point(x, y);
                    controllerJoystickRing.y = variablePosOfJoystick.y;
                    controllerJoystickInnerPart.y = variablePosOfJoystick.y;
                    controllerJoystickInnerDefaultPos.y = variablePosOfJoystick.y;
                }
                else if (punchButtonDown)
                {
                    controller_punch.y = y;
                }
            }
        }
        else if (currentScreen == Constants.SCREEN_CONTROLLER_WRT_PLAYER_PLACEMENT)
        {
            if (punchButtonDown)
            {
                controller_punch.x = x;
                controller_punch.y = y;
            }
        }
        else if (currentScreen == Constants.SCREEN_BUTTON_PLACEMENT_SCREEN)
        {
            if (x > GameManager.screenWidth * 2 / 100 && x < GameManager.screenWidth * 98 / 100
                    && y > GameManager.screenHeight * 20 / 100 && y < GameManager.screenHeight * 96 / 100)
            {
                if (moveButtonLeft)
                {
                    controller_left.x = x;
                    controller_left.y = y;
                }
                else if (moveButtonRight)
                {
                    controller_right.x = x;
                    controller_right.y = y;
                }
                else if (moveButtonUp)
                {
                    controller_up.x = x;
                    controller_up.y = y;
                }
                else if (moveButtonDown)
                {
                    controller_down.x = x;
                    controller_down.y = y;
                }
                else if (punchButtonDown)
                {
                    controller_punch.x = x;
                    controller_punch.y = y;
                }
            }
        }
    }

    public int calculateDistanceSquared(int x1, int y1, int x2, int y2)
    {
        return (((x1 - x2) * (x1 - x2)) + ((y1 - y2) * (y1 - y2)));
    }

    public void onBackKey()
    {
        if (currentScreen == Constants.SCREEN_RUNNING)
        {
            pauseMusicAndSound();
            currentScreen = Constants.SCREEN_PAUSE;
        }
        else if (currentScreen == Constants.SCREEN_PAUSE)
        {
            currentScreen = Constants.SCREEN_RUNNING;
        }
    }

    public void handleSwipe(int direction)
    {
        message += "swipe:" + direction;
    }

    public void pause()
    {
        pauseMusicAndSound();
        if (currentScreen == Constants.SCREEN_RUNNING)
        {

            currentScreen = Constants.SCREEN_PAUSE;
        }
    }

    private void pauseMusicAndSound()
    {
        if (Game.isSoundEnabled && gameMusic != null)
        {
            gameMusic.pause();
        }
        if (Door.doorOpen)
        {
            if (Game.isSoundEnabled && AlarmSiren != null)
            {
                AlarmSiren.pause();
            }
        }

        if (Door.doorOpen)
        {
            Door.tempTime = Door.currentTimeElapsed;
        }

    }

    public void resume()
    {
        resumeMusicAndSound();
    }

    private void resumeMusicAndSound()
    {
        if (Game.isSoundEnabled)
        {
            if (Door.doorOpen)
            {
                if (AlarmSiren != null)
                {
                    AlarmSiren.resume();
                }
            }
            if (gameMusic != null)
            {
                gameMusic.resume();
            }
        }
    }

    public void deallocate()
    {
        if (Door.doorOpen)
        {
            stopAlarm();
        }
        else
        {
            if (gameMusic != null)
            {
                gameMusic.stop();
                gameMusic.unload();
            }
        }
        controllerWRTPlayer = null;
        resumeButtonPositionPauseScreen = quitButtonPositionPauseScreen = restartButtonPositionPauseScreen = controlsButtonPositionPauseMenu = null;
        restartPositionGameOverScreen = quitPositionGameOverScreen = null;
        controller_left = controller_right = controller_up = controller_down = controller_touched = controller_punch = null;
        tipstring = null;
        progressBarWidth = 0;
        GameObjectManager.gameObjectList.removeAllElements();
        controller_pressed = false;
        map = null;
        Laser.laserOff = false;
        FixedCamera.cameraOffFlag = false;
        Door.doorOpen = false;
        moveLeft = moveRight = moveUp = moveDown = false;
        controller_pressed = false;
        gom = null;
        player.deallocate();

        BitmapCacher.deallocateAllImages();
        mousePointerPosition = null;
        mousePointer = null;
        leftMouseupdate = rightMouseUpdate = upMouseUpdate = downMouseUpdate = false;
        if (currentLevel > Game.levelsUnlocked)
        {
            Game.levelsUnlocked = currentLevel;
        }
        backButtonPosition = null;

        controllerJoystickRing = controllerJoystickInnerPart = controllerJoystickInnerDefaultPos = null;
        loadingScreenBackground = null;

        if (currentLevel == FREEDOM_LEVEL)
        {
            freedomBackground = backButton = null;
        }
    }

    public void deallocatedBeforeNextLevel()
    {
        GameObjectManager.gameObjectList.removeAllElements();
        controller_pressed = false;
        map = null;
        Laser.laserOff = false;
        FixedCamera.cameraOffFlag = false;
        Door.doorOpen = false;
        moveLeft = moveRight = moveUp = moveDown = false;
        controller_pressed = false;
        gom = new GameObjectManager();
        tileMapSetup();
    }

    public void accelerometerData(double x, double y, double z, double min, double max)
    {
    }

    private void restartGame()
    {
        GameObjectManager.gameObjectList.removeAllElements();
        map = null;
        player.deallocate();

        if (Door.doorOpen)
        {
            stopAlarm();
        }
        else
        {
            if (gameMusic != null)
            {
                gameMusic.stop();
                gameMusic.unload();
            }

        }
        loadMusic();
        Laser.laserOff = false;
        FixedCamera.cameraOffFlag = false;
        Door.doorOpen = false;
        moveLeft = moveRight = moveUp = moveDown = false;
        usingJoystick = false;
        if (PlatformService.isTouchSupported())
        {
            controllerJoystickInnerPart.y = controllerJoystickInnerDefaultPos.y;
            controllerJoystickInnerPart.x = controllerJoystickInnerDefaultPos.x;
        }

        controller_pressed = false;
        gom = new GameObjectManager();
        tileMapSetup();

        currentScreen = Constants.SCREEN_RUNNING;
        GameManager.isLoadingScreen = false;
    }

    private void loadNextLevel()
    {
        if (Door.doorOpen)
        {
            stopAlarm();
        }
        else
        {
            if (gameMusic != null)
            {
                gameMusic.stop();
                gameMusic.unload();
            }

        }
        loadMusic();
        currentLevel++;
        progressBarWidth = 20 * progressBarSize / 100;
        usingJoystick = false;
        GameObjectManager.gameObjectList.removeAllElements();
        controller_pressed = false;
        map = null;
        if (PlatformService.isTouchSupported())
        {
            controllerJoystickInnerPart.x = controllerJoystickInnerDefaultPos.x;
            controllerJoystickInnerPart.y = controllerJoystickInnerDefaultPos.y;
        }
        progressBarWidth = 40 * progressBarSize / 100;
        keyReleased(Constants.RIGHTKEY);
        keyReleased(Constants.LEFTKEY);
        keyReleased(Constants.UPKEY);
        keyReleased(Constants.DOWNKEY);
        keyReleased(Constants.KEYNUM_7);
        progressBarWidth = 50 * progressBarSize / 100;
        Laser.laserOff = false;
        FixedCamera.cameraOffFlag = false;
        Door.doorOpen = false;
        moveLeft = moveRight = moveUp = moveDown = false;
        gom = new GameObjectManager();
        tileMapSetup();
        progressBarWidth = 70 * progressBarSize / 100;
        progressBarWidth = 80 * progressBarSize / 100;
        if (currentLevel == FREEDOM_LEVEL)
        {
            freedomBackground = new Bitmap("/images/gameplay/freedom.png");
            backButton = new Bitmap("/images/menu/back.png");
            backButtonPosition = new Point(0, GameManager.screenHeight - backButton.getHeight());
//                        aboutButtonPosition = new Point(GameManager.screenWidth * 72 / 100, GameManager.screenHeight * 70 / 100);

            currentScreen = Constants.SCREEN_FREEDOM;
        }
        else
        {
            currentScreen = Constants.SCREEN_RUNNING;
        }
        progressBarWidth = 100 * progressBarSize / 100;
        GameManager.isLoadingScreen = false;
    }

    public void levelCleared()
    {
        progressBarWidth = 0;
        currentScreen = Constants.SCREEN_LEVEL_CLEAR;
    }

    private void implementSingleControllerKeyPressed(int pointerID, int x, int y)
    {
        if (x > controller_punch.x - BitmapCacher.controller_punch.getWidth() / 2
                && x < controller_punch.x + BitmapCacher.controller_punch.getWidth() * 3 / 2
                && y > controller_punch.y - BitmapCacher.controller_punch.getWidth() / 2
                && y < controller_punch.y + BitmapCacher.controller_punch.getHeight() * 3 / 2)
        {
            punchTouchPointerID = pointerID;
            controller_pressed = true;
            controller_touched = controller_punch;
            GamePlayView.instance.player.punch();
        }
        else
        {

            if (x > controller_left.x - BitmapCacher.controller_left.getWidth()
                    && x < controller_left.x + BitmapCacher.controller_left.getWidth() * 200 / 100
                    && y > controller_left.y - BitmapCacher.controller_left.getWidth() * 50 / 100
                    && y < controller_left.y + BitmapCacher.controller_left.getHeight() * 150 / 100)
            {
                navKeyTouchPointerID = pointerID;
                controller_pressed = true;
                controller_touched = controller_left;
                GamePlayView.instance.player.moveLeft();
            }
            else if (x > controller_right.x - BitmapCacher.controller_left.getWidth()
                    && x < controller_right.x + BitmapCacher.controller_right.getWidth() * 200 / 100
                    && y > controller_right.y - BitmapCacher.controller_left.getWidth() * 50 / 100
                    && y < controller_right.y + BitmapCacher.controller_right.getHeight() * 150 / 100)
            {
                navKeyTouchPointerID = pointerID;
                controller_pressed = true;
                controller_touched = controller_right;
                GamePlayView.instance.player.moveRight();
            }
            else if (x > controller_up.x - BitmapCacher.controller_left.getWidth()
                    && x < controller_up.x + BitmapCacher.controller_up.getWidth() * 200 / 100
                    && y > controller_up.y - BitmapCacher.controller_left.getWidth()
                    && y < controller_up.y + BitmapCacher.controller_up.getHeight() * 200 / 100)
            {
                navKeyTouchPointerID = pointerID;
                controller_pressed = true;
                controller_touched = controller_up;
                GamePlayView.instance.player.moveUp();
            }
            else if (x > controller_down.x - BitmapCacher.controller_left.getWidth()
                    && x < controller_down.x + BitmapCacher.controller_down.getWidth() * 200 / 100
                    && y > controller_down.y - BitmapCacher.controller_left.getWidth()
                    && y < controller_down.y + BitmapCacher.controller_down.getHeight() * 200 / 100)
            {
                navKeyTouchPointerID = pointerID;
                controller_pressed = true;
                controller_touched = controller_down;
                GamePlayView.instance.player.moveDown();
            }
        }

    }

    private void implementTouchWRTPlayerController(int pointerID, int x, int y)
    {
        if (x > controller_punch.x - BitmapCacher.controller_punch.getWidth() / 2
                && x < controller_punch.x + BitmapCacher.controller_punch.getWidth() * 3 / 2
                && y > controller_punch.y - BitmapCacher.controller_punch.getWidth() / 2
                && y < controller_punch.y + BitmapCacher.controller_punch.getHeight() * 3 / 2)
        {
            punchTouchPointerID = pointerID;
            controller_pressed = true;
            controller_touched = controller_punch;
            GamePlayView.instance.player.punch();
        }
        else
        {
            navKeyTouchPointerID = pointerID;
            int angle = (int) angleBetween2Lines(player.position, controllerWRTPlayer);
            if (angle > 45 && angle < 135)
            {
                GamePlayView.instance.player.moveDown();
            }
            else if (angle >= 135 && angle < 225)
            {
                GamePlayView.instance.player.moveLeft();
            }
            else if (angle >= 225 && angle < 315)
            {
                GamePlayView.instance.player.moveUp();
            }
            else if ((angle >= 315 && angle < 360) || (angle >= 0 && angle <= 45))
            {
                GamePlayView.instance.player.moveRight();
            }
        }
    }

    public void playAlarm()
    {
        if (gameMusic != null)
        {
            gameMusic.stop();
            gameMusic.unload();
        }

        try
        {
            AlarmSiren = new Music(100, "/music/police_siren", -1);
        }
        catch (Exception ex)
        {
            ex.printStackTrace();
        }
        if (Game.isSoundEnabled && AlarmSiren != null)
        {
            AlarmSiren.play();
        }
    }

    private void stopAlarm()
    {
        if (Game.isSoundEnabled && AlarmSiren != null)
        {
            AlarmSiren.stop();
            AlarmSiren.unload();
        }
    }

    public double angleBetween2Lines(Point guard, Point player)
    {
        int baseAngle;
        Vector2 v1 = new Vector2(guard.x, guard.y);
        Vector2 v2 = new Vector2(player.x, player.y);
        Vector2 difference = Vector2.getDifferenceVector(v2, v1);
        difference.Normalize();
        if (difference.x == 0)
        {
            baseAngle = 90;
        }
        else
        {
            baseAngle = Utility.getInverseTan(Math.abs(difference.y / difference.x));
        }

        if (difference.x < 0 && difference.y > 0)   //2nd quadrant
        {
            baseAngle = 90 + 90 - baseAngle;
        }
        else if (difference.x < 0 && difference.y < 0)   //3rd quadrant
        {
            baseAngle += 180;
        }
        else if (difference.x > 0 && difference.y < 0)   //4th quadrant
        {
            baseAngle = 270 + 90 - baseAngle;
        }
        return baseAngle;
    }
}
